package IO;

import MapClass.Studnt;

import java.io.*;

public class ObjectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建Java对象
        Student student =new Student(111,"王富贵");
        //创建序列化
        ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("D:\\BanGong\\TestFile\\temp.txt"));
        //序列化对象
        oos.writeObject(student);
        //刷新
        oos.flush();
        //关闭流
        oos.close();
        //反序列化
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("D:\\BanGong\\TestFile\\temp.txt"));
        Object obj =ois.readObject();//反序列化回来一个学生对象
        System.out.println(obj);
        ois.close();
    }
}
//参与序列化的类必须实现这个接口才行
class Student implements Serializable {
    //IDEA自动生成版本序列号
    private static final long serialVersionUID = 3663646290069564650L;
    private int no;
    private String name;

    public Student() {
    }
    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "Sdutend学生{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}